package cn.rokhdelar.ams.management.mapper;

import cn.rokhdelar.ams.management.domain.po.SysDepartment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 针对表【sys_department(部门表)】的数据库操作Mapper
 * @author rokhdelar
 * @since  2024-10-02 13:08:48
 * @version 1.0.0
 */
public interface SysDepartmentMapper extends BaseMapper<SysDepartment> {
    //  递归查询部门及其子部门的ID
    @Select("WITH RECURSIVE subDepartments AS (" +
            "SELECT id, parent_id FROM sys_department WHERE id = #{departmentId} " +
            "UNION ALL " +
            "SELECT d.id, d.parent_id FROM sys_department d " +
            "INNER JOIN subDepartments sd ON d.parent_id = sd.id" +
            ") " +
            "SELECT id FROM subDepartments")
    List<String> getSubDepartmentIds(@Param("departmentId") String departmentId);

}




